-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

newTalentType{ type="viral/abilities", name = "viral abilities", description = "Viral abilities" }

newTalent{
	name = "Spit",
	type = {"viral/abilities", 1},
	points = 5,
	range = function(self, t) return math.floor(5 + self:getTalentLevel(t)*2) end,
	message = "@Source@ spits green goo!",
	requires_target = true,
	targets = function(self, t)
		return {
			{
				type = "bolt",
				range = self:getTalentRange(t),
				radius = 1,
				talent = t,
				display = {
					display = "*",
					color=colors.GREEN,
					trail = "viral_trail",
				},
			}
		}
	end,
	action = function(self, t)
		local targets = self:resolveTalentTargets(t)
		local tg = targets[1].typ
		local x = targets[1].x
		local y = targets[1].y
		if not x or not y then return false end
		
		-- Spit away
		local dam = 2 + self:getTalentLevel(t)
		self:projectile(tg, x, y, DamageType.VIRAL, math.max(0, dam))
		return true
	end,
	info = function(self, t)
		return ([[Spit a globule of virus.]])
	end,
}

newTalent{
	name = "Explode",
	type = {"viral/abilities", 1},
	points = 5,
	range = function(self, t) return math.floor(self:getTalentLevel(t)) end,
	message = false,
	action = function(self, t)
		-- Acquire a target within range
		local tg = {type="bolt", range=self:getTalentRange(t), radius=1, talent=t}
		local x, y, target = self:getTarget(tg)
		if not x or not y or not target then return nil end
		if math.floor(core.fov.distance(self.x, self.y, x, y)) > self:getTalentRange(t) then return nil end
		game.logSeen(self, "%s explodes into a fine, green mist!", self.name:capitalize())
		-- Fire in the hole
		local radius = self:getTalentLevel(t)/3 + 1
		local duration = 3
		local dam = rng.range(self:getTalentLevel(t), self:getTalentLevel(t)*10)
		game.level.map:addEffect(self, self.x, self.y, duration,
			DamageType.VIRAL, dam, radius, 5, nil,
			{type="viral", trail="viral_trail"}, nil, false)
		self:die()
		return true
	end,
	info = function(self, t)
		return ([[Explode and release viral gas.]])
	end,
}

newTalent{
	name = "Web Spit",
	type = {"viral/abilities", 1},
	points = 5,
	range = function(self, t) return math.floor(5 + self:getTalentLevel(t)*2) end,
	message = "@Source@ spits a tangle of green sticky strands!",
	requires_target = true,
	targets = function(self, t)
		return {
			{
				type = "bolt",
				range = self:getTalentRange(t),
				radius = 1,
				talent = t,
				display = {
					display = "*",
					color=colors.GREEN,
					trail = "viral_trail",
				},
			}
		}
	end,
	action = function(self, t)
		local targets = self:resolveTalentTargets(t)
		local tg = targets[1].typ
		local x = targets[1].x
		local y = targets[1].y
		if not x or not y then return false end
		
		-- Spit away
		-- Spit away
		local dam = 1 + self:getTalentLevel(t)
		self:projectile(tg, x, y, function(tx, ty)
			local grids = self:project(tg, tx, ty, function(txx, tyy)
				DamageType:get(DamageType.VIRAL).projector(self, txx, tyy, DamageType.VIRAL, dam)
				local actor = game.level.map(txx, tyy, engine.Map.ACTOR)
				if actor then actor:setEffect(actor.EFF_SLOWED, 10, {}) end
			end, nil, {type="viral", trail="viral_trail"})
		end)
		return true
	end,
	info = function(self, t)
		return ([[Spit a web of viral strands.]])
	end,
}